<?

/**
* Use $_SESSION["GDate"] if you are using custom date
* or just use the system date
*
* $today_date = $_SESSION["GDate"];
* $today_date = date("d-m-Y");
*
*/
$today_date		  = date("d-m-Y");

$thisSequence   = "GROUP_SEQ";

$srcDbPos      = "POS";
$srcDbDept     = "DEPARTMENT";
$srcDbHdr      = "GROUP_HDR";
$srcDbDtl      = "GROUP_DTL";
$srcDbHdrLog   = "LOG_GROUP_HDR";
$srcDbSequence = "MST_SEQUENCE";

$UID     = unserialize($_SESSION["serUID"]);

$userId  = $UID->getUID();

$Db = new oracleDb;
$Db->init($_SESSION["ORCLU"],
           $_SESSION["OPASS"],
           $_SESSION["SID"],
           _NO_COMMENT);

if($Db->connect()){

	$CLFetchers->fetchPosId($Db,
                          $_POST["usrPosEnt"],
                          $srcDbPos,
                          $posId);

  $CLFetchers->fetchDeptId($Db,
                           $_POST["usrDeptEnt"],
                           $srcDbDept,
                           $deptId);

	$CLFetchers->fetchNewGroupId($Db,
	                             $posId,
	                             $deptId,
	                             $srcDbHdr,
	                             $isExists,
	                             $groupId);

	if($isExists){

	  $Misc->usrDieJs("Error: An Existing template has been created");
	}else{

	  /* Do nothing */
	}

	$nAccess = sizeof($_POST["levelMnEnt"]);

  for($iRow = 0; $iRow < $nAccess; $iRow++){

    if($_POST["entryEnt"][$iRow]   != '' ||
       $_POST["modifyEnt"][$iRow]  != '' ||
       $_POST["deleteEnt"][$iRow]  != '' ||
       $_POST["viewEnt"][$iRow]    != '' ||
       $_POST["finalizeEnt"][$iRow]!= '' ||
       $_POST["approvedEnt"][$iRow]!= '' ||
       $_POST["journalEnt"][$iRow] != '' ||
       $_POST["programsEnt"][$iRow]!= '' ||
       $_POST["pictEnt"][$iRow]    != ''){

      $values = "  ".($iRow + 1)." ,                  ".
                "  ".$groupId." ,                     ".
                " '".$_POST["levelMnEnt"][$iRow]."',  ".
                " '".$_POST["descpEnt"][$iRow]."',    ".
                "  ".$CLocal->isTrue($_POST["entryEnt"][$iRow])." ,     ".
                "  ".$CLocal->isTrue($_POST["modifyEnt"][$iRow])." ,    ".
                "  ".$CLocal->isTrue($_POST["deleteEnt"][$iRow])." ,    ".
                "  ".$CLocal->isTrue($_POST["viewEnt"][$iRow])." ,      ".
                "  ".$CLocal->isTrue($_POST["finalizeEnt"][$iRow])." ,  ".
                "  ".$CLocal->isTrue($_POST["approvedEnt"][$iRow])." ,  ".
                "  ".$CLocal->isTrue($_POST["journalEnt"][$iRow])." ,   ".
                "  ".$CLocal->isTrue($_POST["programsEnt"][$iRow])." ,  ".
                "  ".$CLocal->isTrue($_POST["pictEnt"][$iRow])."        ";

      $CLHandlers->insToDtl($Db,
                            $values,
                            $srcDbDtl);
    }else{

      /* Do nothing */
    }
  }

  $values = "  ".$groupId." , ".
            "  ".$posId." ,   ".
            "  ".$deptId." ,  ".
            "  1              "; /* status default to 1 */

  $CLHandlers->insToHdr($Db,
                        $values,
                        $srcDbHdr);

	$Db->commit();

	if($Db->getStatus()){

	  $Misc->usrMsg("Upload Complete");
	}else{

	  $Misc->usrDieJs("Upload Process Interupted");
	}

	$Db->dc();

	/**
	* Pending...
	* I'm not sure if this
	* reset procedure off
	* causes buggy
	*
	*	$restore  = 0;
	* $uprun    = 0;
	* $dataup   = 0;
	*
	*/

	//jumpTo(2,"../".$_SESSION["source_caller"]);

}else{

	$Misc->usrDieJs("Db connection failed");
}

?>